﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SortGuid.Tool.Comb.Strategy;

namespace SortGuid.Tool.Comb
{
    public class SqlCombProvider : BaseCombProvider
    {
        private const int EmbedAtIndex = 10;

        public SqlCombProvider(ICombDateTimeStrategy dateTimeStrategy, TimestampProvider customTimestampProvider = null, GuidProvider customGuidProvider = null)
            : base(dateTimeStrategy, customTimestampProvider, customGuidProvider)
        {
        }

        public override Guid Create(Guid value, DateTime timestamp)
        {
            byte[] array = value.ToByteArray();
            byte[] sourceArray = _dateTimeStrategy.DateTimeToBytes(timestamp);
            Array.Copy(sourceArray, 0, array, 10, _dateTimeStrategy.NumDateBytes);
            return new Guid(array);
        }

        public override DateTime GetTimestamp(Guid comb)
        {
            byte[] sourceArray = comb.ToByteArray();
            byte[] array = new byte[_dateTimeStrategy.NumDateBytes];
            Array.Copy(sourceArray, 10, array, 0, _dateTimeStrategy.NumDateBytes);
            return _dateTimeStrategy.BytesToDateTime(array);
        }
    }
}
